/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.unal.bioingenium.kbmed.knowledge.dao;

import co.edu.unal.bioingenium.kbmed.knowledge.KnowledgeSourceMetaData;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Alejandro Riveros Cruz
 */
public class KnowledgeSourceDAOFactory {

    private static Map<String, KnowledgeSourceDAO> KnowledgeSourceDAOMap = new HashMap<String, KnowledgeSourceDAO>();

    /**
     *
     * @return
     */
    public static KnowledgeSourceDAO getKnowledgeSourceDAO() {
        KnowledgeSourceMetaData knowledgeSourceMetaData = KnowledgeSourceMetaData.getReference();
        if (KnowledgeSourceDAOMap.containsKey(knowledgeSourceMetaData.getIdentifier())) {
            KnowledgeSourceDAO knowledgeSourceDAO = KnowledgeSourceDAOMap.get(knowledgeSourceMetaData.getIdentifier());
            knowledgeSourceDAO.checkStatus();
            return knowledgeSourceDAO;
        } else {
            KnowledgeSourceDAO knowledgeSourceDAO;
            try {
                Class c = Class.forName(knowledgeSourceMetaData.getDAOClass().getName());
                knowledgeSourceDAO = (KnowledgeSourceDAO) c.newInstance();
                KnowledgeSourceDAOMap.put(knowledgeSourceMetaData.getIdentifier(), knowledgeSourceDAO);
                return knowledgeSourceDAO;
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(KnowledgeSourceDAOFactory.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                Logger.getLogger(KnowledgeSourceDAOFactory.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(KnowledgeSourceDAOFactory.class.getName()).log(Level.SEVERE, null, ex);
            }
            return null;
        }
    }
}
